package com.xzcedu.billsystem;


import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author 徐州城
 * @version 1.0
 * 主要的程序 核心代码在这
 * 分析
 * 1. 创建变量用来保存 必要的信息
 * 2. 还需要创建一个保存数据的成员 KeepData 用来保存数据到文件中
 * 3. 还要创建一个字符串 方便后续的读取
 */
@SuppressWarnings("all")
public class MainProcedure {
    private double price;//价格
    private String cause;//原因
    private static double money;//剩余的钱
    private static String information = "";//用来专门显示信息
    Date date = new Date();//用来显示时间
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//用于格式化日期


    //退出的时候 把money的值赋给endMoney 方便后续读取
    private static double endMoney;
    private Scanner scanner = new Scanner(System.in);//标准输入流


    //用来计算账单
    public void calcBill() {
        System.out.print("您需要继续上一次的记录吗(y/n)：");
        String str = scanner.next();//接收信息 需要执行哪一个方法
        boolean flag = true;//用于 判断循环的开始或结束

        switch (str) {
            case "y":
                if (KeepData.getFile().exists()) {//如果该文件存在就执行该命令
                    KeepData.readData();//执行该方法读取文件

                    do {
                        String key = showBill();//得到选择的选项
                        flag = selectFileMethod(key);//返回值赋给flag 判断是否要退出或继续
                    } while (flag);
                    break;
                } else
                    System.out.println("您没有上一次记录! 请您重新开始新的记录...");
            case "n":
                System.out.print("请输入您的余额：");
                money = scanner.nextDouble();//把总金额放入 money
                do {
                    String key = showBill();//得到选择的选项
                    flag = selectMethod(key);//返回值赋给flag 判断是否要退出或继续
                } while (flag);
                break;
        }

    }

    //用来显示菜单的方法
    public String showBill() {
        System.out.println();
        System.out.println("-------------欢迎光临账单管理系统-------------\r\n" +
                "           1. 账单明细\r\n" +
                "           2. 收益入账\r\n" +
                "           3. 消   费\r\n" +
                "           4. 退   出\r\n");
        System.out.print("请输入您的选项：");
        return scanner.next();
    }

    //用来普通的方式解决 选择选项的方法
    public boolean selectMethod(String key) {
        switch (key) {
            case "1":
                System.out.println("账单明细：");
                if (!(this.information == null)) {
                    System.out.println(this.information);
                } else
                    System.out.println("当前无账单明细!");
                break;
            case "2":
                System.out.print("收益原因：" + "\r");
                cause = scanner.next();
                System.out.print("收入金额：");
                price = scanner.nextInt();
                this.money += price;

                date = new Date();//获取当前时间
                this.information += "原因：" + cause + "\t\t收入情况：" + price + "\t\t余额：" + money +  "\t\t" + sdf.format(date) + "\r\n";
                break;
            case "3":
                System.out.print("消费原因：" + "\r");
                cause = scanner.next();
                System.out.print("消费金额：");
                price = scanner.nextInt();
                this.money -= price;

                this.information += "原因：" + cause + "\t\t消费情况：" + price + "\t\t余额：" + money + "\t\t"  + sdf.format(date) + "\r\n";
                break;

            case "4":
                System.out.print("请您确认退出y/n：");
                if (scanner.next().equals("n")) {//细节优化
                    break;
                }

                System.out.print("您需要保存信息吗(y/n)？");
                String str = scanner.next();
                if (str.equals("y")) {
                    KeepData.writeDate();//保存信息
                    //退出的时候保存余额
                    MainProcedure.endMoney = MainProcedure.money;
                    System.out.println("保存成功!");
                } else {
                    System.out.println("欢迎下次光临...");
                    MainProcedure.information = "";//用户不保存 那么就把数据置为空
                }
                return false;
            default:
                System.out.println("输入有误");
                break;
        }
        return true;
    }

    //以IO的方式解决 选择的方法
    public boolean selectFileMethod(String key) {
        switch (key) {
            case "1":
                System.out.println("账单明细：");
                if (!(this.information == null)) {
                    System.out.println(MainProcedure.information);
                } else
                    System.out.println("当前无账单明细!");
                break;
            case "2":
                System.out.print("收益原因：" + "\r");
                cause = scanner.next();
                System.out.print("收入金额：");
                price = scanner.nextInt();
                this.money += price;

                this.information += "原因：" + cause + "\t\t收入情况：" + price + "\t\t余额：" + money + "\t\t" + sdf.format(date) + "\r\n";
                break;
            case "3":
                System.out.print("消费原因：" + "\r");
                cause = scanner.next();
                System.out.print("消费金额：");
                price = scanner.nextInt();
                this.money -= price;

                this.information += "原因：" + cause + "\t\t消费情况：" + price + "\t\t余额：" + money + "\t\t" + sdf.format(date) + "\r\n";
                break;
            case "4":
                System.out.print("请您确认退出y/n：");
                if (scanner.next().equals("n")) {//细节优化
                    break;
                }

                System.out.print("您需要保存信息吗(y/n)？");
                String str = scanner.next();
                if (str.equals("y")) {
                    KeepData.writeDate();//保存信息
                    //退出的时候保存余额
                    MainProcedure.endMoney = MainProcedure.money;
                    System.out.println("保存成功!");
                } else {
                    System.out.println("欢迎下次光临...");
                    MainProcedure.information = "";//用户不保存 那么就把数据置为空
                }
                return false;
            default:
                System.out.println("输入有误");
                break;
        }
        return true;
    }


    public static String getInformation() {
        return information;
    }

    public static void setInformation(String information) {
        MainProcedure.information = information;
    }

    public static double getMoney() {
        return money;
    }

    public static void setMoney(double money) {
        MainProcedure.money = money;
    }
}
